package com.kwok.rbac.application.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kwok.rbac.application.common.utils.R;
import com.kwok.rbac.application.entity.Department;
import com.kwok.rbac.application.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2024-05-24
 */
@RestController
@RequestMapping("/department")
@Slf4j
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 添加单个部门
     * @param department
     * @return
     */
    @PostMapping("/save")
    public R saveDep(@RequestBody Department department){
        return R.write(departmentService.save(department));
    }

    /**
     * 批量保存部门
     * @param departmentList
     * @return
     */
    @PostMapping("/batchSave")
    public R batchSaveDep(@RequestBody List<Department> departmentList){
        return R.write(departmentService.saveBatch(departmentList));
    }

    /**
     * 根据主键查询单个部门信息
     * @param dNumber
     * @return
     */
    @GetMapping("/get/{dNumber}")
    public R getDep(@PathVariable("dNumber") Integer dNumber){
        Department department = departmentService.getById(dNumber);
        log.debug("查询到的部门信息为:{}",department);
        return R.success(department);
    }

    /**
     * 查询所有部门
     * @return
     */
    @GetMapping("/list")
    public R depList(){
        List<Department> departmentList = departmentService.list();
        log.debug("查询到的部门信息为:{}",departmentList);
        return R.success(departmentList);
    }

    /**
     * 部门分页查询
     * @param page
     * @return
     */
    @PostMapping("/depPage")
    public R depPage(@RequestBody Page<Department> page){
        Page<Department> departmentPage = departmentService.page(page);
        log.debug("查询到的部门信息为:{}",departmentPage);
        return R.success(departmentPage);
    }

    /**
     * 根据主键修改单个部门信息
     * @param department
     * @return
     */
    @PutMapping("/update")
    public R updateDep(@RequestBody Department department){
        return R.write(departmentService.updateById(department));
    }

    /**
     * 根据主键删除单个部门
     * @param dNumber
     * @return
     */
    @DeleteMapping("/{dNumber}")
    public R removeDep(@PathVariable Integer dNumber){
        return R.write(departmentService.removeById(dNumber));
    }

    /**
     * 根据主键批量删除部门
     * @param dNumbers
     * @return
     */
    @DeleteMapping
    public R batchRemoveDep(@RequestBody List<Integer> dNumbers){
        return R.write(departmentService.removeBatchByIds(dNumbers));
    }

}
